package com.dk.controller;

import com.dk.api.ItemServiceAPI;
import com.dk.api.WarehouseServiceAPI;
import com.dk.pojo.CodePart;
import com.dk.pojo.MdItem;
import com.dk.pojo.Warehouse;
import com.dk.pojo.WmItemRecptLine;
import com.dk.result.Result;
import com.dk.result.ResultConstant;
import com.dk.service.RecptLineService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author LTZ
 * @create 2023/5/11
 */
@RestController
@RequestMapping("recptLine")
@Api(tags = "入库信息接口文档")
public class RecptLineController {
    @Autowired
    private RecptLineService recptLineService;
    @Autowired
    private ItemServiceAPI itemServiceAPI;
    @Autowired
    private WarehouseServiceAPI warehouseServiceAPI;

    @GetMapping("getRecptLineInfo")
    @ApiOperation("入库信息查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "recptId",value = "入库单id",required = true)
    })
    public Result getRecptLineInfo(Long recptId){
        List<WmItemRecptLine> result = recptLineService.getRecptLineInfo(recptId);
        return new Result(ResultConstant.CODE,ResultConstant.MESSAGE,result);
    }

    @PostMapping("addRecptLine")
    @ApiOperation("入库信息增加")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "recptId",value = "入库单ID",required = true),
            @ApiImplicitParam(name = "itemId",value = "产品物料ID"),
            @ApiImplicitParam(name = "itemCode",value = "产品物料编码",required = true),
            @ApiImplicitParam(name = "itemName",value = "产品物料名称"),
            @ApiImplicitParam(name = "specification",value = "规格型号"),
            @ApiImplicitParam(name = "unitOfMeasure",value = "单位"),
            @ApiImplicitParam(name = "quantityRecived",value = "入库数量",required = true),
            @ApiImplicitParam(name = "batchCode",value = "入库批次号",required = true),
            @ApiImplicitParam(name = "warehouseId",value = "仓库ID"),
            @ApiImplicitParam(name = "warehouseCode",value = "仓库编码"),
            @ApiImplicitParam(name = "warehouseName" , value = "仓库名称" , required = true),
            @ApiImplicitParam(name = "expireDate" , value = "有效期" , required = true),
            @ApiImplicitParam(name = "remark" , value = "备注"),
            @ApiImplicitParam(name = "token" , value = "登录后返回的token串" , required = true),
    })
    public Result addRecptLine(WmItemRecptLine recptLine, @RequestHeader String token){
        if(token == null || "".equals(token)){
            return new Result(ResultConstant.TOKEN_CODE, ResultConstant.TOKEN_MESSAGE);
        }
        MdItem item = itemServiceAPI.selectByItemCode(recptLine.getItemCode());
        Warehouse warehouse = warehouseServiceAPI.selectByWarehouseName(recptLine.getWarehouseName());
        if(item==null || warehouse==null){
            return new Result(ResultConstant.IQC_ADD_NULL, ResultConstant.IQC_ADD_MESSAGE);
        }
        return recptLineService.addRecptLine(recptLine,item,warehouse,token);
    }

    @DeleteMapping("deleteRecptLine")
    @ApiOperation("入库信息删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lineId", value = "入库信息id",required = true)
    })
    public Result deleteRecptLine(Long lineId){
        recptLineService.deleteRecptLine(lineId);
        return new Result(ResultConstant.CODE,ResultConstant.MESSAGE);
    }

}